package 双指针.有效三角形的个数;

import java.util.Arrays;

public class NumTrian {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {2,2,3,10};
        System.out.println(so.triangleNumber(arr));
    }
}
class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int num = 0;
        int left = 0,right;
        for(int i = nums.length - 1; i >= 0; i--){
            right = i-1;
            num +=SearchNum(nums,left,right,i);
        }
        return  num;
    }
    public static int SearchNum(int[] nums,int left,int right,int i) {
        int num = 0;
        int Max = nums[i];
        while(left<right){
            if(nums[left] + nums[right] > Max){
                num+=right-left;
                right--;
            }else{
                left++;
            }
        }
        return num;
    }
}